package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.annotation.IsLoginAnno;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.OrderTime;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.UserContext;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class VideoAuthController {
        @Autowired
        private ILoginInfoService loginInfoService;
        @Autowired
        private IOrderTimeService orderTimeService;
        @Autowired
        private IVideoAuthService videoAuthService;
        @Autowired
        private IUserInfoService userInfoService;


        @RequestMapping("videoAuditOrder")
        @IsLoginAnno
        public String videoAuditOrder(Model model,String date){
                //再次访问判断有没有认证通过
                UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());
                if(userInfo.hasVedioAuth()){
                        model.addAttribute("videoSuccess",true);
                }else if(userInfo.getVideoAuthId()!=null){
                        model.addAttribute("videoAuditOrder",videoAuthService.getById(userInfo.getVideoAuthId()));
                }else{
                        //查出审核员
                        List<LoginInfo> auditors = loginInfoService.listAuditors();
                        model.addAttribute("auditors",auditors);
                        //查出预约日期,集合添加
                        List<Date> orderDates = new ArrayList<>();
                        orderDates.add(DateUtils.addDays(new Date(), 0));
                        orderDates.add(DateUtils.addDays(new Date(), 1));//返回当前日期的后一天
                        orderDates.add(DateUtils.addDays(new Date(), 2));
                        orderDates.add(DateUtils.addDays(new Date(), 3));
                        model.addAttribute("orderDates",orderDates);
                        //查出能审核的时间段

                        if(date!=null){
                                Calendar c = Calendar.getInstance();
                                int i = c.get(Calendar.DATE);
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                                try {
                                        Date parse = format.parse(date);//拿到前台传来的参数,解析成时间格式
                                        c.setTime(parse);//传到日历对象
                                        int i1 = c.get(Calendar.DATE);//拿到参数的天数
                                        if(i==i1){//对比,如果是当天的预约
                                                c.setTime(new Date());
                                                int i21 = c.get(Calendar.HOUR_OF_DAY);//拿到小时
                                                List<OrderTime> orderTimes =  orderTimeService.orderTimesqueryOrderTime(i21);
                                                model.addAttribute("orderTimes",orderTimes);
                                        }else{//如果不是当天的预约,就返回所有时间段
                                                List<OrderTime> orderTimes = orderTimeService.selectAll();
                                                model.addAttribute("orderTimes",orderTimes);
                                                model.addAttribute("or",date);
                                        }
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }

                               /* System.out.println(i);
                                System.out.println(i1);*/
                        }else{
                                Calendar c1 = Calendar.getInstance();
                                c1.setTime(new Date());
                                int i2 = c1.get(Calendar.HOUR_OF_DAY);//拿到小时
                                List<OrderTime> orderTimes1 =  orderTimeService.orderTimesqueryOrderTime(i2);
                                model.addAttribute("orderTimes",orderTimes1);
                        }

                        /*c.get(Calendar.HOUR_OF_DAY)*/

                       /* if(date=){

                        }*/
                       /* List<OrderTime> orderTimes = orderTimeService.selectAll();
                        model.addAttribute("orderTimes",orderTimes);*/
                }
                return "videoOrder";
        }

        //保存视频认证申请的方法
        @RequestMapping("saveVideoAuditOrder")
        @ResponseBody
        public JSONResult saveVideoAuditOrder(VideoAuth videoAuth){
                JSONResult jsonResult = new JSONResult();
                try {
                        videoAuthService.saveVideoAuditOrder(videoAuth);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

        /**
         * 取消预约的方法
         * @return
         */
        @RequestMapping("cancelVideoAuth")
        @ResponseBody
        public JSONResult cancelVideoAuth(){
                JSONResult jsonResult = new JSONResult();
                UserInfo userInfo = userInfoService.getById(UserContext.getLoginInfo().getId());
                try {
                        videoAuthService.cancelVideoAuth(userInfo.getVideoAuthId());
                        userInfo.setVideoAuthId(null);
                        userInfoService.update(userInfo);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

        @RequestMapping("another")
        public String another(Model model,String date){
                if(date!=null){
                        Calendar c = Calendar.getInstance();
                        int i = c.get(Calendar.DATE);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                        try {
                                Date parse = format.parse(date);//拿到前台传来的参数,解析成时间格式
                                c.setTime(parse);//传到日历对象
                                int i1 = c.get(Calendar.DATE);//拿到参数的天数
                                if(i==i1){//对比,如果是当天的预约
                                        c.setTime(new Date());
                                        int i21 = c.get(Calendar.HOUR_OF_DAY);//拿到小时
                                        List<OrderTime> orderTimes =  orderTimeService.orderTimesqueryOrderTime(i21);
                                        model.addAttribute("orderTimes",orderTimes);
                                }else{//如果不是当天的预约,就返回所有时间段
                                        List<OrderTime> orderTimes = orderTimeService.selectAll();
                                        model.addAttribute("orderTimes",orderTimes);
                                }
                        } catch (Exception e) {
                                e.printStackTrace();
                        }

                               /* System.out.println(i);
                                System.out.println(i1);*/
                }
                return "videoOrder";
        }

}
